package org.example.DesignPatterns.Mediator;

import java.util.Objects;

/**
 * @description:
 * @author: farprt
 * @create: 2022-09-24 08:52
 **/
public class User {

    private String name;

    private ChatRoom chatRoom;

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void login(ChatRoom chatRoom){
        chatRoom.register(this);
        this.chatRoom=chatRoom;
    }

    public void logout(){
        this.chatRoom.deregister(this);
        this.chatRoom=null;
    }

    public void talk(User to,String msg){
        if(null==this.chatRoom){
            System.out.println("【"+name+"的对话框】"+"您还没有登录");
            return;
        }
        chatRoom.sendMsg(this,to,msg);
    }

    public void listen(User from,User to,String msg){
        System.out.print("【"+this.name+"的对话框】");
        System.out.println(chatRoom.processMsg(from,to,msg));
    }

    @Override
    public boolean equals(Object obj) {
        if(obj==null || getClass()!= obj.getClass()){
            return false;
        }
        User user=(User) obj;
        return Objects.equals(name,user.name);
    }
}